package cn.itcast.concurrent.thread;

import java.util.Random;

public class TestFutureTask {

    public static void main(String[] args) throws Exception {
        java.util.concurrent.FutureTask<String> task = new java.util.concurrent.FutureTask<>(new MyCallable());
        new Thread(task).start();

        while (true) {
            if(task.isCancelled()){
                throw new RuntimeException("任务已经取消");
            }
            if (task.isDone()) {
                System.out.println("任务完成:" + task.get());
                break;
            }
            Thread.sleep(1000);
        }
    }
}


class MyCallable implements java.util.concurrent.Callable<String> {
    @Override
    public String call() throws Exception {
        int time = new Random().nextInt(10) + 5;
        Thread.sleep(1000 * time);
        return "Hello";
    }

}


